package com.krisyu.flyweightPattern.template;

import java.util.HashMap;

/**
 * @DATE 2022/1/10
 * @Author KrisYu
 * @Description
 */
public class FlyweightFactory {
    // 定义一个池容器
    private static HashMap<String, Flyweight> pool = new HashMap<>();
    // 享元工厂
    public static Flyweight getFlyweight(String Extrinsic){
        // 需要返回的向
        Flyweight flyweight = null;
        if(pool.containsKey(Extrinsic)){
            flyweight = pool.get(Extrinsic);
            System.out.println("-----------直接从池中获取对象，key = " + Extrinsic + "----------------");
        }else{
            flyweight = new ConcreteFlyweight1(Extrinsic);
            pool.put(Extrinsic, flyweight);
            System.out.println("------------------------新建一个对象，并放入池中----------------");
        }
        return flyweight;
    }
}
